/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.business.customer.core.domain.partner.action;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.crypto.asymmetric.RSA;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import info.peigen.hotpot.business.customer.core.domain.partner.Partner;
import info.peigen.hotpot.business.customer.core.domain.partner.PartnerPermission;
import info.peigen.hotpot.business.customer.core.domain.partner.PartnerTech;
import info.peigen.hotpot.business.customer.core.repository.entity.PartnerEntity;
import info.peigen.hotpot.business.customer.facade.info.PartnerManageInfo;
import info.peigen.hotpot.business.customer.facade.info.partner.PartnerInfo;
import info.peigen.hotpot.business.customer.facade.info.partner.PartnerPermissionInfo;
import info.peigen.hotpot.business.customer.facade.info.partner.PartnerTechInfo;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <b>(PartnerConverter)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2022/5/24
 */
@Slf4j
public class PartnerConverter {
    public static PartnerEntity convert(Partner domain) {
        PartnerEntity entity = BeanUtil.copyProperties(domain, PartnerEntity.class, "permission", "platform", "tech");
        entity.setPermission(JSON.toJSONString(domain.getPermission()))
                .setTech(JSON.toJSONString(domain.getTech(), JSONWriter.Feature.WriteEnumsUsingName));
        return entity;
    }

    public static PartnerInfo convertInfo(Partner domain) {
        return BeanUtil.copyProperties(domain, PartnerInfo.class);
    }

    public static PartnerManageInfo convertManageInfo(PartnerEntity entity) {
        return BeanUtil.copyProperties(entity, PartnerManageInfo.class, "tech", "permission");
    }

    public static PartnerInfo convertInfo(PartnerEntity entity) {
        return BeanUtil.copyProperties(entity, PartnerInfo.class, "tech", "permission")
                .setPermission(JSON.parseObject(entity.getPermission(), PartnerPermissionInfo.class))
                .setTech(JSON.parseObject(entity.getTech(), PartnerTechInfo.class));
    }

    public static List<PartnerInfo> convertInfo(List<PartnerEntity> entities) {
        return entities.stream().map(entity -> BeanUtil.copyProperties(entity, PartnerInfo.class, "permission", "platform", "tech")).collect(Collectors.toList());
    }

    public static Partner convert(PartnerEntity entity) {
        return BeanUtil.copyProperties(entity, Partner.class, "permission", "platform", "tech")
                .setPermission(JSON.parseObject(entity.getPermission(), PartnerPermission.class))
                .setTech(convert(entity.getTech()));
    }

    public static Partner convert(PartnerInfo info) {
        return BeanUtil.copyProperties(info, Partner.class);
    }

    private static PartnerTech convert(String tech) {
        PartnerTech partnerTech = JSON.parseObject(tech, PartnerTech.class);
        // Partner Key
        RSA rsa = new RSA(partnerTech.getPartnerPrivateKeyStr(), partnerTech.getPartnerPublicKeyStr());
        partnerTech.setPartnerPrivateKey(rsa.getPrivateKey())
                .setPartnerPublicKey(rsa.getPublicKey());
        // Hotpot Key
        rsa = new RSA(partnerTech.getHotpotPrivateKeyStr(), partnerTech.getHotpotPublicKeyStr());
        partnerTech.setHotpotPrivateKey(rsa.getPrivateKey())
                .setHotpotPublicKey(rsa.getPublicKey());
        return partnerTech;
    }
}